package com.well.register.console.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.well.register.console.dao.JobExecutorMapper;
import com.well.register.console.domain.Job;
import com.well.register.console.domain.JobExecutor;
import com.well.register.console.domain.JobParam;
import com.well.register.console.model.job.QueryJobExecutorParam;
import com.well.register.console.service.JobExecutorService;
import com.well.register.console.service.JobParamService;
import com.well.register.console.service.JobService;
import com.well.rpc.model.DoJobResponseModel;
import com.well.rpc.model.RegisterModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  job service impl
 * </p>
 *
 * @author weedo
 * @since 2022/8/7
 */

@Service
public class JobExecutorServiceImpl extends ServiceImpl<JobExecutorMapper, JobExecutor> implements JobExecutorService {

    @Resource
    private JobExecutorMapper jobExecutorMapper;
    @Resource
    private JobService jobService;
    @Resource
    private JobParamService jobParamService;

    private static final int SUCCESS = 0;
    private static final int FAIL = 1;

    @Override
    public int saveExecutorInfo(String jobName, RegisterModel registerModel, Integer runType) {
        JobExecutor executor = new JobExecutor();
        Job job = jobService.getJob(jobName, registerModel);
        executor.setJobId(job.getId());
        executor.setJobName(jobName);
        //手动or自动
        executor.setType(runType);
        jobExecutorMapper.insert(executor);
        return executor.getId();
    }

    @Override
    public Job getJobInfo(RegisterModel registerModel, String jobName) {
        return jobService.getJob(jobName, registerModel);
    }

    @Override
    public boolean updateJob(Job job, int thread_key) {
        UpdateWrapper<Job> wrapper = new UpdateWrapper<>();
        wrapper.eq(true, "thread_key", thread_key);
        wrapper.eq(true, "id", job.getId());
        return jobService.update(job, wrapper);
    }

    @Override
    public boolean saveExecutorResult(DoJobResponseModel responseModel) {
        JobExecutor executor = this.getById(responseModel.getJobId());
        executor.setResult(responseModel.getResult());
        if (responseModel.getResult() == FAIL) {
            executor.setMsg(responseModel.getMsg());
        }
        executor.setUpdateTime(new Date());
        return this.updateById(executor);
    }

    @Override
    public Page<JobExecutor> getExecutorRecordsByJobId(QueryJobExecutorParam param) {
        QueryWrapper<JobExecutor> wrapper = new QueryWrapper<>();
        Page<JobExecutor> page = new Page<>();
        page.setCurrent(param.getCurrent());
        page.setSize(param.getPageSize());
        wrapper.eq("job_id", param.getId());
        if (param.getResult() != null) {
            wrapper.eq("result", param.getResult());
        }
        wrapper.orderByDesc("create_time");
        Page<JobExecutor> executorPage = this.page(page, wrapper);
        return executorPage;
    }

    @Override
    public List<JobParam> getJobParam(Integer id) {
        return jobParamService.getJobParams(id);
    }
}
